package com.huanwei.questionfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.huanwei.cc.base.MyApplication;
import com.huanwei.cc.R;
import com.huanwei.cc.bean.Life_MSG2;
import com.huanwei.cc.bean.MyEvent;
import com.huanwei.cc.db.Z_DHD_Dao;
import com.huanwei.cc.ui.EditTextDialog3;
import com.huanwei.questionfragment.adapter.Waite_Answered_Adapter;

import java.util.List;

import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * Created by zhuwenhai on 2017/12/1.
 * 待回答
 */

public class DHD_Fragment extends Fragment {




    public static DHD_Fragment newInstance() {
        Bundle args = new Bundle();
        DHD_Fragment fragment = new DHD_Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    Z_DHD_Dao z_msgDao;
    ListView lvList;
    Waite_Answered_Adapter bank_card_adapter;

    View view;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_share, null);
            ButterKnife.bind(this, view);
            initViewsAndEvents();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;
    }


    public void initViewsAndEvents() {
        EventBus.getDefault().register(this);

        lvList = (ListView) view.findViewById(R.id.lv_list);//设置;
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        z_msgDao = new Z_DHD_Dao(MyApplication.getInstance());
        List<Life_MSG2> query = z_msgDao.query();
        bank_card_adapter = new Waite_Answered_Adapter();
//        bank_card_adapter.setactivity((MainActivity) getActivity());
        lvList.setAdapter(bank_card_adapter);
        bank_card_adapter.addItem(query);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("shua")) {
                List<Life_MSG2> query = z_msgDao.query();
                lvList.setAdapter(bank_card_adapter);
                bank_card_adapter.clear();
                bank_card_adapter.addItem(query);
            }
        }
    }

    private void shareDialog(String name, String zhiye) {

        EditTextDialog3 dialog = new EditTextDialog3(name, zhiye);
        dialog.show(getActivity().getSupportFragmentManager());
    }
}